package net.zjitc.servlet;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

/**
 * Created by hasee on 2017/9/26.
 */
@WebServlet(name = "CountServlet",urlPatterns = "/hs")
public class CountServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out =response.getWriter();
        Integer count=0;
        ServletContext context = this.getServletContext();
        count =(Integer) context.getAttribute("count");
       if(count==null){
           count=0;
           count++;
           context.setAttribute("count",count);
       }else {
           count++;
           context.setAttribute("count",count);
       }
        out.write("第"+count+"次访问");
    }
}
